<?php

defined('_JEXEC') or die;

function get_image_list_from_article($article_content) {

//get list tour details
    $pattern = '~[^{}]++|\G\{(?<nested>(?R)*+)\}~';
    preg_match_all($pattern, $article_content, $matches/* , PREG_SET_ORDER */);
    $image_lists = $matches['nested'][1];
    $img_list = explode(" ", $image_lists);
    $img_list = $img_list[0];
    $cat_id = explode(':', $img_list);
    $cat_id = $cat_id[1];

    //get list from db
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $query->select('g.*, cg.catpath as catpath');
    $query->from($db->quoteName('#__joomgallery', 'g'));
    $query->join('INNER', $db->quoteName('#__joomgallery_catg', 'cg') . ' ON (' . $db->quoteName('g.catid') . ' = ' . $db->quoteName('cg.cid') . ')');
    $query->where($db->quoteName('g.catid') . '=' . $cat_id . ' and g.published = 1');
    $db->setQuery($query);
    $result = $db->loadObjectList();
    return $result;
}

function get_tours_of_tours_page($tour_id) {

    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $query->select('*');
    $query->from($db->quoteName('#__categories'));
    $query->where($db->quoteName('parent_id') . '=' . $tour_id . ' and id not in (10,11)');
    $db->setQuery($query);
    $result = $db->loadObjectList();
    return $result;
}

function check_cat_have_article($catid = '') {
    if (!empty($catid)) {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('c.*, ca.title as catname');
        $query->from($db->quoteName('#__content', 'c'));
        $query->join('INNER', $db->quoteName('#__categories', 'ca') . ' ON (' . $db->quoteName('c.catid') . ' = ' . $db->quoteName('ca.id') . ')');
        $query->where('c.state = 1 and c.catid = ' . $catid . '');

        $db->setQuery($query);
        $result = $db->loadObjectList();

        return $result;
    }
}

function get_cat_from_parent($id = '') {
    if (!empty($id)) {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('*');
        $query->from($db->quoteName('#__categories'));
        $query->where($db->quoteName('parent_id') . '=' . $id . '');
        $db->setQuery($query);
        $result = $db->loadObjectList();
        return $result;
    }
}

function CathomeInfo($catid = '') {
    if (!empty($catid)) {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('*');
        $query->from($db->quoteName('#__categories'));
        $query->where($db->quoteName('id') . '=' . '' . $catid . '');
        $db->setQuery($query);
        $result = $db->loadObject();
        return $result;
    }
}

function get_article_info($id) {
    if (!empty($id)) {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('c.*');
        $query->from($db->quoteName('#__content', 'c'));     
        $query->where('c.state = 1 and c.id = ' . $id . '');
        $db->setQuery($query);
        $result = $db->loadObject();
        return $result;
    }
}

function get_cat_title_from_article($id) {
    if (!empty($id)) {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('c.*, ca.title as catname');
        $query->from($db->quoteName('#__content', 'c'));
        $query->join('INNER', $db->quoteName('#__categories', 'ca') . ' ON (' . $db->quoteName('c.catid') . ' = ' . $db->quoteName('ca.id') . ')');
        $query->where('c.state = 1 and c.id = ' . $id . '');
        $db->setQuery($query);
        $result = $db->loadObject();
        return $result;
    }
}

function get_article_from_cat($catid = '') {
    if (!empty($catid)) {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('*');
        $query->from($db->quoteName('#__content'));
        $query->where($db->quoteName('catid') . '=' . '' . $catid . ' and state = 1');
        $db->setQuery($query);
        $result = $db->loadObjectList();
        return $result;
    }
}

function get_feature_article() {
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $query->select('*');
    $query->from($db->quoteName('#__content', 'c'));
    $query->join('INNER', $db->quoteName('#__content_frontpage', 'f') . ' ON (' . $db->quoteName('c.id') . ' = ' . $db->quoteName('f.content_id') . ')');
    $query->where('state = 1 and catid IN (10,11)');
    $db->setQuery($query);
    $result = $db->loadObjectList();
    return $result;
}

function get_slide_show_images($catid = '') {
    if (!empty($catid)) {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('*');
        $query->from($db->quoteName('#__phocagallery'));
        $query->where('published = 1 and catid = ' . $catid . '');
        $db->setQuery($query);
        $result = $db->loadObjectList();
        return $result;
    }
}

function trimstr($string, $length = 25, $method = 'WORDS', $pattern = '...') {

    if (!is_numeric($length)) {
        $length = 25;
    }

    if (strlen($string) <= $length) {
        return rtrim($string) . $pattern;
    }

    $truncate = substr($string, 0, $length);

    if ($method != 'WORDS') {
        return rtrim($truncate) . $pattern;
    }

    if ($truncate[$length - 1] == ' ') {
        return rtrim($truncate) . $pattern;
    }
    // we got ' ' right where we want it

    $pos = strrpos($truncate, ' ');
    // lets find nearest right ' ' in the truncated string

    if (!$pos) {
        return $pattern;
    }
    // no ' ' (one word) or it resides at the very begining 
    // of the string so the whole string goes to the toilet

    return rtrim(substr($truncate, 0, $pos)) . $pattern;
    // profit
}

function getYouTubeIdFromURL($url) {
    preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches);
    return isset($matches[1]) ? $matches[1] : false;
}

function get_first_image($str) {
    $output = preg_match_all('/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]*>/i', $str, $matches);
    return $matches[1][0];
}

function cutstring($input, $width) {
    if (empty($input))
        return $input;

    if (strlen($input) <= $width) {
        return $input;
    }

    $output = substr($input, 0, $width);

    //normals words are seldom more than 30 chars
    $pos = 0;
    $found = false;

    for ($i = $width; $i >= 0; $i--) {
        if (ctype_space($output[$i])) {
            $found = true;
            break;
        }
        $pos++;
    }

    if ($found && ($pos > 0)) {
        $output = substr($output, 0, ($width - $pos));
        $output = rtrim($output);
    }

    $output .= '...';
    return $output;
}

/* $converted_value = ConverCurrency(1,"usd","eur");
  echo $converted_value; */

/**
 * 'formatcurrency' Function to convert your floating int into a
 * @author Joel Peterson - @joelasonian - www.joelpeterson.com
 * @param flatcurr	float	integer to convert
 * @param curr	string of desired currency format
 * @return formatted number
 */
function formatcurrency($floatcurr, $curr = "USD") {
    $currencies['ARS'] = array(2, ',', '.');   //	Argentine Peso
    $currencies['AMD'] = array(2, '.', ',');   //	Armenian Dram
    $currencies['AWG'] = array(2, '.', ',');   //	Aruban Guilder
    $currencies['AUD'] = array(2, '.', ' ');   //	Australian Dollar
    $currencies['BSD'] = array(2, '.', ',');   //	Bahamian Dollar
    $currencies['BHD'] = array(3, '.', ',');   //	Bahraini Dinar
    $currencies['BDT'] = array(2, '.', ',');   //	Bangladesh, Taka
    $currencies['BZD'] = array(2, '.', ',');   //	Belize Dollar
    $currencies['BMD'] = array(2, '.', ',');   //	Bermudian Dollar
    $currencies['BOB'] = array(2, '.', ',');   //	Bolivia, Boliviano
    $currencies['BAM'] = array(2, '.', ',');   //	Bosnia and Herzegovina, Convertible Marks
    $currencies['BWP'] = array(2, '.', ',');   //	Botswana, Pula
    $currencies['BRL'] = array(2, ',', '.');   //	Brazilian Real
    $currencies['BND'] = array(2, '.', ',');   //	Brunei Dollar
    $currencies['CAD'] = array(2, '.', ',');   //	Canadian Dollar
    $currencies['KYD'] = array(2, '.', ',');   //	Cayman Islands Dollar
    $currencies['CLP'] = array(0, '', '.');   //	Chilean Peso
    $currencies['CNY'] = array(2, '.', ',');   //	China Yuan Renminbi
    $currencies['COP'] = array(2, ',', '.');   //	Colombian Peso
    $currencies['CRC'] = array(2, ',', '.');   //	Costa Rican Colon
    $currencies['HRK'] = array(2, ',', '.');   //	Croatian Kuna
    $currencies['CUC'] = array(2, '.', ',');   //	Cuban Convertible Peso
    $currencies['CUP'] = array(2, '.', ',');   //	Cuban Peso
    $currencies['CYP'] = array(2, '.', ',');   //	Cyprus Pound
    $currencies['CZK'] = array(2, '.', ',');   //	Czech Koruna
    $currencies['DKK'] = array(2, ',', '.');   //	Danish Krone
    $currencies['DOP'] = array(2, '.', ',');   //	Dominican Peso
    $currencies['XCD'] = array(2, '.', ',');   //	East Caribbean Dollar
    $currencies['EGP'] = array(2, '.', ',');   //	Egyptian Pound
    $currencies['SVC'] = array(2, '.', ',');   //	El Salvador Colon
    $currencies['ATS'] = array(2, ',', '.');   //	Euro
    $currencies['BEF'] = array(2, ',', '.');   //	Euro
    $currencies['DEM'] = array(2, ',', '.');   //	Euro
    $currencies['EEK'] = array(2, ',', '.');   //	Euro
    $currencies['ESP'] = array(2, ',', '.');   //	Euro
    $currencies['EUR'] = array(2, ',', '.');   //	Euro
    $currencies['FIM'] = array(2, ',', '.');   //	Euro
    $currencies['FRF'] = array(2, ',', '.');   //	Euro
    $currencies['GRD'] = array(2, ',', '.');   //	Euro
    $currencies['IEP'] = array(2, ',', '.');   //	Euro
    $currencies['ITL'] = array(2, ',', '.');   //	Euro
    $currencies['LUF'] = array(2, ',', '.');   //	Euro
    $currencies['NLG'] = array(2, ',', '.');   //	Euro
    $currencies['PTE'] = array(2, ',', '.');   //	Euro
    $currencies['GHC'] = array(2, '.', ',');   //	Ghana, Cedi
    $currencies['GIP'] = array(2, '.', ',');   //	Gibraltar Pound
    $currencies['GTQ'] = array(2, '.', ',');   //	Guatemala, Quetzal
    $currencies['HNL'] = array(2, '.', ',');   //	Honduras, Lempira
    $currencies['HKD'] = array(2, '.', ',');   //	Hong Kong Dollar
    $currencies['HUF'] = array(0, '', '.');   //	Hungary, Forint
    $currencies['ISK'] = array(0, '', '.');   //	Iceland Krona
    $currencies['INR'] = array(2, '.', ',');   //	Indian Rupee
    $currencies['IDR'] = array(2, ',', '.');   //	Indonesia, Rupiah
    $currencies['IRR'] = array(2, '.', ',');   //	Iranian Rial
    $currencies['JMD'] = array(2, '.', ',');   //	Jamaican Dollar
    $currencies['JPY'] = array(0, '', ',');   //	Japan, Yen
    $currencies['JOD'] = array(3, '.', ',');   //	Jordanian Dinar
    $currencies['KES'] = array(2, '.', ',');   //	Kenyan Shilling
    $currencies['KWD'] = array(3, '.', ',');   //	Kuwaiti Dinar
    $currencies['LVL'] = array(2, '.', ',');   //	Latvian Lats
    $currencies['LBP'] = array(0, '', ' ');   //	Lebanese Pound
    $currencies['LTL'] = array(2, ',', ' ');   //	Lithuanian Litas
    $currencies['MKD'] = array(2, '.', ',');   //	Macedonia, Denar
    $currencies['MYR'] = array(2, '.', ',');   //	Malaysian Ringgit
    $currencies['MTL'] = array(2, '.', ',');   //	Maltese Lira
    $currencies['MUR'] = array(0, '', ',');   //	Mauritius Rupee
    $currencies['MXN'] = array(2, '.', ',');   //	Mexican Peso
    $currencies['MZM'] = array(2, ',', '.');   //	Mozambique Metical
    $currencies['NPR'] = array(2, '.', ',');   //	Nepalese Rupee
    $currencies['ANG'] = array(2, '.', ',');   //	Netherlands Antillian Guilder
    $currencies['ILS'] = array(2, '.', ',');   //	New Israeli Shekel
    $currencies['TRY'] = array(2, '.', ',');   //	New Turkish Lira
    $currencies['NZD'] = array(2, '.', ',');   //	New Zealand Dollar
    $currencies['NOK'] = array(2, ',', '.');   //	Norwegian Krone
    $currencies['PKR'] = array(2, '.', ',');   //	Pakistan Rupee
    $currencies['PEN'] = array(2, '.', ',');   //	Peru, Nuevo Sol
    $currencies['UYU'] = array(2, ',', '.');   //	Peso Uruguayo
    $currencies['PHP'] = array(2, '.', ',');   //	Philippine Peso
    $currencies['PLN'] = array(2, '.', ' ');   //	Poland, Zloty
    $currencies['GBP'] = array(2, '.', ',');   //	Pound Sterling
    $currencies['OMR'] = array(3, '.', ',');   //	Rial Omani
    $currencies['RON'] = array(2, ',', '.');   //	Romania, New Leu
    $currencies['ROL'] = array(2, ',', '.');   //	Romania, Old Leu
    $currencies['RUB'] = array(2, ',', '.');   //	Russian Ruble
    $currencies['SAR'] = array(2, '.', ',');   //	Saudi Riyal
    $currencies['SGD'] = array(2, '.', ',');   //	Singapore Dollar
    $currencies['SKK'] = array(2, ',', ' ');   //	Slovak Koruna
    $currencies['SIT'] = array(2, ',', '.');   //	Slovenia, Tolar
    $currencies['ZAR'] = array(2, '.', ' ');   //	South Africa, Rand
    $currencies['KRW'] = array(0, '', ',');   //	South Korea, Won
    $currencies['SZL'] = array(2, '.', ', ');   //	Swaziland, Lilangeni
    $currencies['SEK'] = array(2, ',', '.');   //	Swedish Krona
    $currencies['CHF'] = array(2, '.', '\'');   //	Swiss Franc	
    $currencies['TZS'] = array(2, '.', ',');   //	Tanzanian Shilling
    $currencies['THB'] = array(2, '.', ',');   //	Thailand, Baht
    $currencies['TOP'] = array(2, '.', ',');   //	Tonga, Paanga
    $currencies['AED'] = array(2, '.', ',');   //	UAE Dirham
    $currencies['UAH'] = array(2, ',', ' ');   //	Ukraine, Hryvnia
    $currencies['USD'] = array(2, '.', ',');   //	US Dollar
    $currencies['VUV'] = array(0, '', ',');   //	Vanuatu, Vatu
    $currencies['VEF'] = array(2, ',', '.');   //	Venezuela Bolivares Fuertes
    $currencies['VEB'] = array(2, ',', '.');   //	Venezuela, Bolivar
    $currencies['VND'] = array(0, '', '.');   //	Viet Nam, Dong
    $currencies['ZWD'] = array(2, '.', ' ');   //	Zimbabwe Dollar

    function formatinr($input) {
        //CUSTOM FUNCTION TO GENERATE ##,##,###.##
        $dec = "";
        $pos = strpos($input, ".");
        if ($pos === false) {
            //no decimals	
        } else {
            //decimals
            $dec = substr(round(substr($input, $pos), 2), 1);
            $input = substr($input, 0, $pos);
        }
        $num = substr($input, -3); //get the last 3 digits
        $input = substr($input, 0, -3); //omit the last 3 digits already stored in $num
        while (strlen($input) > 0) { //loop the process - further get digits 2 by 2
            $num = substr($input, -2) . "," . $num;
            $input = substr($input, 0, -2);
        }
        return $num . $dec;
    }

    if ($curr == "INR") {
        return formatinr($floatcurr);
    } else {
        return number_format($floatcurr, $currencies[$curr][0], $currencies[$curr][1], $currencies[$curr][2]);
    }
}

/*
  formatcurrency(1000045.25);				//1,000,045.25 (USD)
  formatcurrency(1000045.25, "CHF");		//1'000'045.25
  formatcurrency(1000045.25, "EUR");		//1.000.045,25
  formatcurrency(1000045, "JPY");			//1,000,045
  formatcurrency(1000045, "LBP");			//1 000 045
  formatcurrency(1000045.25, "INR");		//10,00,045.25
 */
?>